<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">
    <div class="article-body">

        <div class="article-intro">

            <h1>Java 8  新特性</h1>
            <p>Java 8 (又称为 jdk 1.8) 是 Java 语言开发的一个主要版本。 Oracle 公司于 2014 年 3 月 18 日发布 Java 8 ，它支持函数式编程，新的 JavaScript 引擎，新的日期 API，新的Stream API 等。</p>
            <hr>
            <h2>新特性</h2>
            <p>Java8 新增了非常多的特性，我们主要讨论以下几个：</p>
            <ul class="list">
                <li><p><b>Lambda 表达式</b> − Lambda允许把函数作为一个方法的参数（函数作为参数传递进方法中。</p></li>
                <li><p><b>方法引用</b> −  方法引用提供了非常有用的语法，可以直接引用已有Java类或对象（实例）的方法或构造器。与lambda联合使用，方法引用可以使语言的构造更紧凑简洁，减少冗余代码。</p></li>
                <li><p><b>默认方法</b> − 默认方法就是一个在接口里面有了一个实现的方法。</p></li>
                <li><p><b>新工具</b> − 新的编译工具，如：Nashorn引擎 jjs、 类依赖分析器jdeps。</p></li>
                <li><p><b>Stream API</b> −新添加的Stream API（java.util.stream） 把真正的函数式编程风格引入到Java中。</p></li>
                <li><p><b>Date Time API</b> − 加强对日期与时间的处理。</p></li>
                <li><p><b>Optional 类</b> − Optional 类已经成为 Java 8 类库的一部分，用来解决空指针异常。</p></li>
                <li><p><b>Nashorn, JavaScript 引擎</b> −  Java 8提供了一个新的Nashorn javascript引擎，它允许我们在JVM上运行特定的javascript应用。</p></li>
            </ul>
            <p>更多的新特性可以参阅官网：<a target="_blank" href="http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html">What's New in JDK 8</a>
            </p><p>在关于 Java 8 文章的实例，我们均使用 jdk 1.8 环境，你可以使用以下命令查看当前 jdk 的版本：</p>
            <pre class="prettyprint prettyprinted" style=""><span class="pln">$ java </span><span class="pun">-</span><span class="pln">version
java version </span><span class="str">"1.8.0_31"</span><span class="pln">
</span><span class="typ">Java</span><span class="pun">(</span><span class="pln">TM</span><span class="pun">)</span><span class="pln"> SE </span><span class="typ">Runtime</span><span class="pln"> </span><span class="typ">Environment</span><span class="pln"> </span><span class="pun">(</span><span class="pln">build </span><span class="lit">1.8</span><span class="pun">.</span><span class="lit">0_31</span><span class="pun">-</span><span class="pln">b13</span><span class="pun">)</span><span class="pln">
</span><span class="typ">Java</span><span class="pln"> </span><span class="typ">HotSpot</span><span class="pun">(</span><span class="pln">TM</span><span class="pun">)</span><span class="pln"> </span><span class="lit">64</span><span class="pun">-</span><span class="typ">Bit</span><span class="pln"> </span><span class="typ">Server</span><span class="pln"> VM </span><span class="pun">(</span><span class="pln">build </span><span class="lit">25.31</span><span class="pun">-</span><span class="pln">b07</span><span class="pun">,</span><span class="pln"> mixed mode</span><span class="pun">)</span></pre>
            <hr>
            <h2>编程风格</h2>
            <p>Java 8 希望有自己的编程风格，并与 Java 7 区别开，以下实例展示了 Java 7 和 Java 8 的编程格式：</p>
            <div class="example">
                <h2 class="example">Java8Tester.java 文件代码：</h2>
                <div class="example_code">
                    <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.</span><span class="hl-identifier">Collections</span><span class="hl-code">;
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.</span><span class="hl-identifier">List</span><span class="hl-code">;
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.</span><span class="hl-identifier">ArrayList</span><span class="hl-code">;
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.</span><span class="hl-identifier">Comparator</span><span class="hl-code">;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Java8Tester</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">

      </span><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">String</span><span class="hl-code">&gt; </span><span class="hl-identifier">names1</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">ArrayList</span><span class="hl-code">&lt;</span><span class="hl-identifier">String</span><span class="hl-code">&gt;</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">names1</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Google </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">names1</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Runoob </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">names1</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Taobao </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">names1</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Baidu </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">names1</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Sina </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;

      </span><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">String</span><span class="hl-code">&gt; </span><span class="hl-identifier">names2</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">ArrayList</span><span class="hl-code">&lt;</span><span class="hl-identifier">String</span><span class="hl-code">&gt;</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">names2</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Google </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">names2</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Runoob </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">names2</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Taobao </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">names2</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Baidu </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">names2</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Sina </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;

      </span><span class="hl-identifier">Java8Tester</span><span class="hl-code"> </span><span class="hl-identifier">tester</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Java8Tester</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">使用 Java 7 语法: </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;

      </span><span class="hl-identifier">tester</span><span class="hl-code">.</span><span class="hl-identifier">sortUsingJava7</span><span class="hl-brackets">(</span><span class="hl-identifier">names1</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">names1</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">使用 Java 8 语法: </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;

      </span><span class="hl-identifier">tester</span><span class="hl-code">.</span><span class="hl-identifier">sortUsingJava8</span><span class="hl-brackets">(</span><span class="hl-identifier">names2</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">names2</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">

   </span><span class="hl-comment">//</span><span class="hl-comment"> 使用 java 7 排序</span><span class="hl-comment"></span><span class="hl-code">
   </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">sortUsingJava7</span><span class="hl-brackets">(</span><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">String</span><span class="hl-code">&gt; </span><span class="hl-identifier">names</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">Collections</span><span class="hl-code">.</span><span class="hl-identifier">sort</span><span class="hl-brackets">(</span><span class="hl-identifier">names</span><span class="hl-code">, </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Comparator</span><span class="hl-code">&lt;</span><span class="hl-identifier">String</span><span class="hl-code">&gt;</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
         @</span><span class="hl-identifier">Override</span><span class="hl-code">
         </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">compare</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">s1</span><span class="hl-code">, </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">s2</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">s1</span><span class="hl-code">.</span><span class="hl-identifier">compareTo</span><span class="hl-brackets">(</span><span class="hl-identifier">s2</span><span class="hl-brackets">)</span><span class="hl-code">;
         </span><span class="hl-brackets">}</span><span class="hl-code">
      </span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">

   </span><span class="hl-comment">//</span><span class="hl-comment"> 使用 java 8 排序</span><span class="hl-comment"></span><span class="hl-code">
   </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">sortUsingJava8</span><span class="hl-brackets">(</span><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">String</span><span class="hl-code">&gt; </span><span class="hl-identifier">names</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">Collections</span><span class="hl-code">.</span><span class="hl-identifier">sort</span><span class="hl-brackets">(</span><span class="hl-identifier">names</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-identifier">s1</span><span class="hl-code">, </span><span class="hl-identifier">s2</span><span class="hl-brackets">)</span><span class="hl-code"> -&gt; </span><span class="hl-identifier">s1</span><span class="hl-code">.</span><span class="hl-identifier">compareTo</span><span class="hl-brackets">(</span><span class="hl-identifier">s2</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
                </div>
            </div>
            <p>执行以上脚本，输出结果为：</p>
            <pre class="prettyprint prettyprinted" style=""><span class="pln">$ javac </span><span class="typ">Java8Tester</span><span class="pun">.</span><span class="pln">java
$ java </span><span class="typ">Java8Tester</span><span class="pln">
</span><span class="pun">使用</span><span class="pln"> </span><span class="typ">Java</span><span class="pln"> </span><span class="lit">7</span><span class="pln"> </span><span class="pun">语法:</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Baidu</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span class="typ">Google</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span class="typ">Runoob</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span class="typ">Sina</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span class="typ">Taobao</span><span class="pln"> </span><span class="pun">]</span><span class="pln">
</span><span class="pun">使用</span><span class="pln"> </span><span class="typ">Java</span><span class="pln"> </span><span class="lit">8</span><span class="pln"> </span><span class="pun">语法:</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Baidu</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span class="typ">Google</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span class="typ">Runoob</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span class="typ">Sina</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span class="typ">Taobao</span><span class="pln"> </span><span class="pun">]</span></pre>
            <p>接下来我们将详细为大家简介 Java 8 的新特性：</p>
            <table class="reference">
                <tbody><tr>
                    <th>序号</th>
                    <th>特性</th>

                </tr>
                <tr>
                    <td>1</td>
                    <td><a href="java8-lambda-expressions.html">Lambda 表达式</a></td>
                </tr>

                <tr>
                    <td>2</td>
                    <td><a href="java8-method-references.html">方法引用</a></td>
                </tr>

                <tr>
                    <td>3</td>
                    <td><a href="java8-functional-interfaces.html">函数式接口</a></td>
                </tr>

                <tr>
                    <td>4</td>
                    <td><a href="java8-default-methods.html">默认方法</a></td>
                </tr>

                <tr>
                    <td>5</td>
                    <td><a href="java8-streams.html">Stream</a></td>
                </tr>


                <tr>
                    <td>6</td>
                    <td><a href="java8-optional-class.html">Optional 类</a></td>
                </tr>

                <tr>
                    <td>7</td>
                    <td><a href="java8-nashorn-javascript.html">Nashorn, JavaScript 引擎</a></td>
                </tr>

                <tr>
                    <td>8</td>
                    <td><a href="java8-datetime-api.html">新的日期时间 API</a></td>
                </tr>

                <tr>
                    <td>9</td>
                    <td><a href="java8-base64.html">Base64</a></td>
                </tr>







                </tbody></table>

        </div>

    </div>
</div>

</body>
</html>
